﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Mysoft.App.Appservices.Basic;
using Mysoft.Domain;
using Mysoft.Domain.Basic;
using Mysoft.Dto;

namespace Mysoft.App.Appservices
{
    /// <summary>
    /// 测试
    /// </summary>
    public class TestAppService : AppServiceBase
    {
        private readonly DomainService<TestDomainService> _domainService;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="domainService"></param>
        public TestAppService(DomainService<TestDomainService> domainService, IMapper mapper)
        {
            _domainService = domainService;
            _mapper = mapper;
           
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> GetPageData(TestCondition condition)
        {
            var list = await _domainService.Instance.GetPageData(condition);
            return Success(list);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Add(TestDto dto) 
        {
            if (dto == null) {
                BusinessException($"{nameof(dto)}参数为空");
            }
            Guid id = await _domainService.Instance.Add(dto);
            return Success(id);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Delete(Guid? id) 
        {
            if (!id.HasValue)
            {
                BusinessException($"{nameof(id)}参数为空");
            }
           _domainService.Instance.Delete(id);
            return Success("删除成功");
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<JsonResult> Modify(TestDto dto) {
            if (dto == null)
            {
                BusinessException($"{nameof(dto)}参数为空");
            }
            _domainService.Instance.Modify(dto);
            return Success("修改成功");
        }
    }
}
